package com.example.androidbighomework.SQLiteHelper;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import androidx.annotation.Nullable;

import com.example.androidbighomework.bean.HomeDataBean;

import java.util.ArrayList;
import java.util.Collections;

public class MyHelper extends SQLiteOpenHelper {
    public MyHelper(@Nullable Context context) {
        super(context, "drem.db", null, 2);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE dremcontentinfo(d_id INTEGER PRIMARY KEY AUTOINCREMENT ,u_id INTEGER,dremcontent VARCHAR(20))");
        db.execSQL("CREATE TABLE user(u_id INTEGER PRIMARY KEY AUTOINCREMENT ,account VARCHAR(20) UNIQUE,password INTEGER)");
        db.execSQL("INSERT INTO user VALUES(1,'admin',123)");
        db.execSQL("INSERT INTO dremcontentinfo VALUES(1,1,'我做了一个梦，一个很好玩的梦，我要记录下它。阳光灿烂，一直奔跑，一直跑一直跑一直跑一直跑一直跑一直跑一直跑一直跑。还在跑还在跑还在跑还在跑还在跑还在跑还在跑...全文')");
        db.execSQL("INSERT INTO dremcontentinfo VALUES(2,1,'今天做了一个梦')");

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    //查找dremcontentinfo数据库里的全部信息
    public void find(){
        ArrayList dremContentArrayList=new ArrayList();
//        MyHelper helper=new MyHelper();
        SQLiteDatabase db=getReadableDatabase();
        Cursor cursor=db.query("dremcontentinfo",new String[]{"dremcontent"},null,null,null,null,null);
        if(cursor.getCount()!=0){
            while (cursor.moveToNext()){
                String dremContent=cursor.getString(cursor.getColumnIndex("dremcontent"));
                Log.i("content",dremContent);
                dremContentArrayList.add(dremContent);

            }

        }
        //反转集合
        Collections.reverse(dremContentArrayList);
        String [] stringContentArray= (String[]) dremContentArrayList.toArray(new String[dremContentArrayList.size()]);
        for (int i=0;i<stringContentArray.length;i++){
            Log.i("i",stringContentArray[i]);
        }
        HomeDataBean.getInstance().setHomeContentText(stringContentArray);
        cursor.close();
        db.close();
    }
    public void findAccountDreamContent(int accountId){
        ArrayList dremContentArrayList=new ArrayList();
        SQLiteDatabase db=getReadableDatabase();
        Cursor cursor=db.query("dremcontentinfo",null,"u_id=?",new String[]{accountId+""},null,null,null);
        if(cursor.getCount()!=0){
            while(cursor.moveToNext()){
                String dremContent=cursor.getString(cursor.getColumnIndex("dremcontent"));
                dremContentArrayList.add(dremContent);
            }
        }
        //反转集合
        Collections.reverse(dremContentArrayList);
        String [] stringContentArray= (String[]) dremContentArrayList.toArray(new String[dremContentArrayList.size()]);
        HomeDataBean.getInstance().setHomeContentText(stringContentArray);
        cursor.close();
        db.close();
    }
    //添加数据
    public void insert(int uId,String dremcontent){
        SQLiteDatabase db=getWritableDatabase();
        ContentValues values=new ContentValues();
        values.put("u_id",uId);
        values.put("dremcontent",dremcontent);

        long id=db.insert("dremcontentinfo",null,values);

        Log.i("insert","添加成功66");
        db.close();
    }
}

